﻿using CodeCreator;
using Common;
using iBing.Common.DbHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeTemplate.CodeSql
{
    public partial class FrmSetSql : Form
    {
        public string Sql
        {
            get { return this.txtSql.Text.Trim(); }
        }

        public IDbHelper dbHelper
        {
            get
            {
                DbInfo dbInfo = cmbDbInfo.SelectedItem as DbInfo;
                IDbHelper dbHelper = null;
                if (dbInfo != null)
                {
                    //生成DbHelper
                    switch (dbInfo.Type.ToUpper())
                    {
                        case "MYSQL":
                            dbHelper = new DbHelperForMySql(dbInfo.ConnectionString);
                            break;
                        case "ORACLE":
                            dbHelper = new DbHelperForOracle(dbInfo.ConnectionString);
                            break;
                    }
                }
                return dbHelper;
            }
        }

        public FrmSetSql()
        {
            InitializeComponent();

            string dbConfigFilePath = System.Windows.Forms.Application.StartupPath + "/Config/dbinfo.xml";
            List<DbInfo> dbInfoList = ToolsSerialize.XmlDeserialize<List<DbInfo>>(dbConfigFilePath);
            this.cmbDbInfo.DataSource = dbInfoList;
            this.cmbDbInfo.DisplayMember = "Name";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtSql.Text.Trim() == "")
            {
                MessageBox.Show("请填写SQL！");
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
